/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.energy.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.energy.constant.RedisConstants;
import com.excesys.exsecs.energy.dto.BumDevListDto;
import com.excesys.exsecs.energy.entity.BumDevList;
import com.excesys.exsecs.energy.mapper.BumDevListMapper;
import com.excesys.exsecs.energy.service.BumDevListService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

/**
 * 配电室
 */
@Service
public class BumDevListServiceImpl extends ServiceImpl<BumDevListMapper, BumDevList> implements BumDevListService {
	private final StringRedisTemplate redisTemplate;

	public BumDevListServiceImpl(StringRedisTemplate redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	@Override
	public List<BumDevListDto> queryDevById(BumDevList bumDevList) {
		List<BumDevListDto> devListDtoList =  this.baseMapper.queryDevById(bumDevList);
		for(BumDevListDto dto:devListDtoList) {
			List<BumDevListDto> devListDtoLists = this.baseMapper.queryDevByParentId(Long.valueOf(dto.getId()));
			dto.setDtoList(devListDtoLists);
		}
		return devListDtoList;
	}

	@Override
	public Page setPage(List<BumDevListDto> bumDevListDtos, Page page) {
		if (bumDevListDtos == null) {
			return null;
		}
		int size = bumDevListDtos.size();
		int pageSize = (int) page.getSize();
		if (pageSize > size) {
			pageSize = size;
		}
		int pageCurrent = (int) page.getCurrent();
		if (pageSize != 0) {
			// 求出最大页数，防止currentPage越界
			int maxPage = size % pageSize == 0 ? size / pageSize : size / pageSize + 1;

			if (pageCurrent > maxPage) {
				pageCurrent = maxPage;
			}
		}
		// 当前页第一条数据的下标
		int curIdx = pageCurrent > 1 ? (pageCurrent - 1) * pageSize : 0;

		List pageList = new ArrayList();

		// 将当前页的数据放进pageList
		for (int i = 0; i < pageSize && curIdx + i < size; i++) {
			pageList.add(bumDevListDtos.get(curIdx + i));
		}

		page.setCurrent(pageCurrent).setSize(pageSize).setTotal(bumDevListDtos.size()).setRecords(pageList);
		return page;
	}

	@Override
	public List<BumDevList> queryDelDevById(Long id) {
		return this.baseMapper.queryDelDevById(id);
	}

	@Override
	public List<BumDevList> queryDevByRoomId(Long id) {
		return  this.baseMapper.queryDevByRoomId(id);
	}
}
